//
//  LFBSearchController.m
//  loveFreshBeen
//
//  Created by Feng on 2017/7/12.
//  Copyright © 2017年 feng. All rights reserved.
//

#import "LFBSearchController.h"
#import "UIColor+Addition.h"
#import "LFBSearchResultController.h"
#import <SKTagView.h>

@interface LFBSearchController () <UISearchBarDelegate>

@property(nonatomic,weak) UISearchBar *customSearchBar;

@property(nonatomic,strong) NSArray *colors;

@property(nonatomic,strong) SKTagView *tagView;

@end

@implementation LFBSearchController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.colors = @[@"#7ecef4", @"#84ccc9", @"#88abda", @"#7dc1dd", @"#b6b8de"];
    
    //设置搜索框
    [self setupSearchBar];
    
    //设置热门搜索
    [self setupTagView];
}

- (void)setupSearchBar
{
    //设置navigationBar的背景颜色,44(导航条) + 20 (状态栏)
//    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
//    self.navigationController.navigationBar.translucent = NO;
    
    CGRect mainViewBounds = self.view.bounds;
    UISearchBar *customSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(CGRectGetWidth(mainViewBounds)/2-((CGRectGetWidth(mainViewBounds)-120)/2), CGRectGetMinY(mainViewBounds)+22, CGRectGetWidth(mainViewBounds)-120, 40)];
    self.customSearchBar = customSearchBar;
    NSLog(@"%@",NSStringFromCGRect(customSearchBar.frame));
    //设置代理
    customSearchBar.delegate = self;
    //是否显示取消按钮
    customSearchBar.showsCancelButton = NO;
    //设置搜索框整体的风格
    customSearchBar.searchBarStyle = UISearchBarStyleMinimal;
    //设置占位文字
    customSearchBar.placeholder = @"请输入商品名称";
    //设置光标颜色
    customSearchBar.tintColor = [UIColor lightGrayColor];
    //设置背景颜色
    customSearchBar.barTintColor = [UIColor whiteColor];
    
    self.navigationItem.titleView = customSearchBar;
}

- (void)setupTagView
{
//    self.tagView = [[SKTagView alloc] initWithFrame:[UIScreen mainScreen].bounds];
////    self.tagView = tagView;
//    self.tagView.frame = CGRectMake(0, 64, ScreenWidth, ScreenHeight);
//    [self.view addSubview:self.tagView];
//    
//    self.tagView.lineSpacing = Margin;
//    self.tagView.interitemSpacing = Margin;
//    self.tagView.padding = UIEdgeInsetsMake(Margin, Margin, Margin, Margin);
//    self.tagView.backgroundColor = [UIColor groupTableViewBackgroundColor];
//    
//    NSArray *tagTextArray = @[@"年货大集",@"酸奶",@"水",@"车厘子",@"恰恰瓜子",@"喂他",@"香烟",@"周黑鸭",@"草莓",@"星巴克",@"卤味"];
//    [tagTextArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//        
//        SKTag *tag = [[SKTag alloc] initWithText:obj];
//        [self.tagView addTag:tag];
//        
////        tag.font = [UIFont systemFontOfSize:50];
//        tag.fontSize = 50;
//        tag.cornerRadius = 8;
//        tag.bgColor = [UIColor whiteColor];
//        tag.highlightedBgColor = [UIColor whiteColor];
//        tag.padding = UIEdgeInsetsMake(13.5, 12.5, 13.5, 12.5);
//        
//        __weak SKTagView *weakView = _tagView;
//        
//        weakView.didTapTagAtIndex = ^(NSUInteger index) {
//            
//            _customSearchBar.text = tagTextArray[index];
//        };
//    }];
    
    NSArray *tagTextArray = @[@"年货大集",@"酸奶",@"水",@"车厘子",@"恰恰瓜子",@"喂他",@"香烟",@"周黑鸭",@"草莓",@"星巴克",@"卤味"];
    
    self.tagView = ({
        SKTagView *view = [SKTagView new];
        view.backgroundColor = [UIColor whiteColor];
        view.padding = UIEdgeInsetsMake(12, 12, 12, 12);
        view.interitemSpacing = 15;
        view.lineSpacing = 10;
        __weak SKTagView *weakView = view;
        view.didTapTagAtIndex = ^(NSUInteger index){
//            [weakView removeTagAtIndex:index];
            _customSearchBar.text = tagTextArray[index];
        };
        view;
    });
    [self.view addSubview:self.tagView];
    [self.tagView mas_makeConstraints: ^(MASConstraintMaker *make) {
        make.top.offset(64);
        make.leading.trailing.bottom.offset(0);
    }];
    
    [tagTextArray enumerateObjectsUsingBlock: ^(NSString *text, NSUInteger idx, BOOL *stop) {
        SKTag *tag = [SKTag tagWithText: text];
        tag.textColor = [UIColor whiteColor];
        tag.fontSize = 15;
        //tag.font = [UIFont fontWithName:@"Courier" size:15];
        //tag.enable = NO;
        tag.padding = UIEdgeInsetsMake(13.5, 12.5, 13.5, 12.5);
        tag.bgColor = [UIColor colorWithHexString: self.colors[idx % self.colors.count]];
        tag.cornerRadius = 5;
        [self.tagView addTag:tag];
    }];

}

#pragma - UISearchBarDelegate代理方法 -
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
    NSLog(@"%s",__func__);
    
    return YES;
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    NSLog(@"%s",__func__);
    //跳转到搜索结果
    LFBSearchResultController *searchResultVC = [LFBSearchResultController new];
    [self.navigationController pushViewController:searchResultVC animated:YES];
}

- (void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar
{
    NSLog(@"%s",__func__);
}

@end
